
#include "fig_player_craft.h"



FIGPlayerCraft::ShipSpriteInfo::ShipSpriteInfo() : 
										m_ShipIndex(FIGPlayerCraft::ShipSpriteInfo::CRAFT_MIDDLE),
										m_PositionCounter(0)
{
	
}

void	FIGPlayerCraft::ShipSpriteInfo::Reset(const math::Vec2& Pos)
{
	m_ShipIndex = FIGPlayerCraft::ShipSpriteInfo::CRAFT_MIDDLE;
	m_PositionCounter = 0;
	m_PrevSwayPos =	Pos;
}



void	FIGPlayerCraft::ShipSpriteInfo::Tick(float dt , 
											const math::Vec2& Pos,
											float Rotation
											)
{
	m_Sprites[m_ShipIndex]->Tick(dt);
	const float POSITION_COUNTER_DURATION = 0.08f;
	// LEFT , RIGHT , MIDDLE determination
	if(m_PositionCounter <= 0)
	{
		const float MIN_DIFF_LEFT = -0.005f;
		const float MIN_DIFF_RIGHT = 0.005f;
        
		math::Vec2 Diff = Pos - m_PrevSwayPos;
		Diff.Rotate(-Rotation);
		const float DiffX = Diff.x;
		if( DiffX < MIN_DIFF_LEFT)
		{
			// goinig left
			GoingLeft();
		}
		else if ( DiffX > MIN_DIFF_RIGHT)
		{
			// going right
			GoingRight();
		}
		else
		{
			// going mid
			GoingMid();
		}
        
		m_PositionCounter = POSITION_COUNTER_DURATION;
		m_PrevSwayPos = Pos;
    }
	else
	{
		m_PositionCounter -= dt;
	}
}

void	FIGPlayerCraft::ShipSpriteInfo::GoingLeft()
{
	if( m_ShipIndex <= 0)
		return;
	
	int	Temp = (int)m_ShipIndex;
	Temp--;
	m_ShipIndex = (FIGPlayerCraft::ShipSpriteInfo::CRAFT_SPRITES)Temp;
}

void	FIGPlayerCraft::ShipSpriteInfo::GoingRight()
{
	if( m_ShipIndex >= CRAFT_SPRITE_COUNT-1)
		return;
    
	int	Temp = (int)m_ShipIndex;
	Temp++;
	m_ShipIndex = (FIGPlayerCraft::ShipSpriteInfo::CRAFT_SPRITES)Temp;
}

void	FIGPlayerCraft::ShipSpriteInfo::GoingMid()
{
	int	Temp = (int)m_ShipIndex;	
	
	if(m_ShipIndex < CRAFT_MIDDLE)
		Temp++;	
	else if(m_ShipIndex > CRAFT_MIDDLE)
		Temp--;

	m_ShipIndex = (FIGPlayerCraft::ShipSpriteInfo::CRAFT_SPRITES)Temp;
    
	//m_ShipIndex = CRAFT_MIDDLE;
}

void	FIGPlayerCraft::ShipSpriteInfo::AddSprite(const std::string& Str)
{
	ui::UISprite* Spr = new ui::UISprite();
	Spr->Create(Str);
	m_Sprites.push_back(Spr);
}

FIGPlayerCraft::ShipSpriteInfo::~ShipSpriteInfo()
{
	for(int i = 0; i < (int)m_Sprites.size(); i++)
	{
		UTIL_SAFE_DELETE(m_Sprites[i]);
	}

	m_Sprites.resize(0);
}

